package org.sample.springboot.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.sample.springboot.model.Account;
import org.sample.springboot.model.Contact;
import org.sample.springboot.model.ExtraInfo;
import org.sample.springboot.validator.ValidateHelper;
import org.sample.springboot.validator.group.BusinessGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
@RequestMapping("/account")
public class AccountController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AccountController.class);

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public AccountResponse get() {

		LOGGER.info("Get account.");

		AccountResponse response = new AccountResponse();
		Account account = new Account();

		Contact[] contacts = new Contact[] { createContact("111", "array@home.com"),
				createContact("222", "array@office.com") };
		account.setContacts(contacts);

		List<Contact> contactList = new ArrayList<>();
		contactList.add(createContact("111", "list@home.com"));
		contactList.add(createContact("222", "list@office.com"));
		account.setContactList(contactList);

		Set<Contact> contactSet = new HashSet<>();
		contactSet.add(createContact("111", "set@home.com"));
		contactSet.add(createContact("222", "set@office.com"));
		account.setContactSet(contactSet);

		Map<String, Contact> contactMap = new HashMap<>();
		contactMap.put("home", createContact("111", "map@home.com"));
		contactMap.put("office", createContact("222", "map@office.com"));
		account.setContactMap(contactMap);

		ExtraInfo<Contact> extraContact = new ExtraInfo<>();
		extraContact.setInfo(createContact("333", "extra@mail.com"));
		account.setExtraContact(extraContact);
		
		Map<String, List<Contact>> contactListMap = new HashMap<>();
		List<Contact> homeContactList = new ArrayList<>();
		homeContactList.add(createContact("555", "list0@home.com"));
		homeContactList.add(createContact("666", "list1@home.com"));
		contactListMap.put("home", homeContactList);
		List<Contact> officeContactList = new ArrayList<>();
		officeContactList.add(createContact("777", "list0@office.com"));
		officeContactList.add(createContact("888", "list1@office.com"));
		contactListMap.put("office", officeContactList);
		account.setContactListMap(contactListMap);

		response.setAccount(account);
		return response;
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public AccountResponse post(@RequestBody AccountRequest request) {

		LOGGER.info("Post account.");

		AccountResponse response = new AccountResponse();
		response.setAccount(request.getAccount());
		return response;
	}

	@RequestMapping(method = RequestMethod.PUT)
	@ResponseBody
	public AccountResponse put(@RequestBody AccountRequest account) {

		LOGGER.info("Put account.");

		ValidateHelper validateHelper = new ValidateHelper();
		validateHelper.validate(account, BusinessGroup.class);

		return new AccountResponse();
	}

	private Contact createContact(String phone, String email) {
		Contact contact = new Contact();
		contact.setPhone(phone);
		contact.setEmail(email);
		return contact;
	}

}
